#该文件是对第一步手机的图片做预处理，主要有resize_image()函数和灰度化
import os
import cv2

IMAGE_SIZE = 64

def resize_image(image, height=IMAGE_SIZE, width=IMAGE_SIZE):
    top, bottom, left, right = (0, 0, 0, 0)

    h, w, _ = image.shape

    longest_edge = max(h, w)
    if h < longest_edge:
        dh = longest_edge - h
        top = dh // 2
        bottom = dh - top
    elif w < longest_edge:
        dw = longest_edge - w
        left = dw // 2
        righ = dw - left
    else:
        pass

    BLACK = [0, 0, 0]

    constant = cv2.copyMakeBorder(image, top, bottom, left, right, cv2.BORDER_CONSTANT, value=BLACK)

    return cv2.resize(constant, (height, width))


if __name__ == '__main__':
    path_name = "C:/Users/LCG/Desktop/data/test"#这个为你想要预处理文件的路径
    i = 0
    for dir_item in os.listdir(path_name):
        full_path = os.path.abspath(os.path.join(path_name, dir_item))
        i += 1
        image = cv2.imread(full_path)       #读取出照片
        image = resize_image(image)         #将图片大小转为64*64
        image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)     #将图片转为灰度图
        cv2.imwrite(full_path,image)


